package cn.kent.netty.base.byteBuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

import static cn.kent.netty.base.byteBuf.TestByteBuf.log;

/**
 * 测试ByteBuf的slice 方法 {@link ByteBuf#slice()}
 * slice,实际是一块物理内存,不过是多个ByteBuf
 */
public class TestSlice {
    public static void main(String[] args) {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(10);
        buf.writeBytes(new byte[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'});
        log(buf);
        ByteBuf f1 = buf.slice(0, 5);
        f1.retain();
        ByteBuf f2 = buf.slice(5, 5);
        f2.retain();
        log(f1);
        log(f2);

        System.out.println("释放原有byteBuf内存");
        buf.release();
        log(f1);

        /*f1.setByte(0, 'b');
        log(f1);
        log(buf);*/
    }
}
